package com.sepro.action;


import javax.annotation.Resource;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.xwork.StringUtils;
import com.sepro.action.BaseAction;
import com.sepro.entity.Member;
import com.sepro.exception.InvalidParameterException;
import com.sepro.exception.MemberAlreadyExistException;
import com.sepro.exception.MemberNotExistException;
import com.sepro.exception.PasswordMatchFailedException;
import com.sepro.service.MemberService;

public class MemberAction extends BaseAction {

	private static final long serialVersionUID = 4304045166604034979L;
	
	private String loginname; //登录用户名，可以是username也可以是email
	/**
	 * @return the loginname
	 */
	public String getLoginname() {
		return loginname;
	}


	/**
	 * @param loginname the loginname to set
	 */
	public void setLoginname(String loginname) {
		this.loginname = loginname;
	}


	/**
	 * @return the email
	 */
	public String getEmail() {
		return email;
	}


	/**
	 * @param email the email to set
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	private String username; //注册用户名
	private String email; //注册email
	private String password; //登录或注册密码
	private String memberId; //用户ID
	
	private String errorMessage; //错误信息
	
	/**
	 * @return the errorMessage
	 */
	public String getErrorMessage() {
		return errorMessage;
	}


	/**
	 * @param errorMessage the errorMessage to set
	 */
	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}


	/**
	 * @return the memberId
	 */
	public String getMemberId() {
		return memberId;
	}


	/**
	 * @param memberId the memberId to set
	 */
	public void setMemberId(String memberId) {
		this.memberId = memberId;
	}

	@Resource(name="memberServiceImpl")
	private MemberService memberService;
	
	/**
	 * @return the username
	 */
	public String getUsername() {
		return username;
	}


	/**
	 * @param username the username to set
	 */
	public void setUsername(String username) {
		this.username = username;
	}


	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}


	/**
	 * @param password the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	public String goLogin(){
		return SUCCESS;
	}
	
	/**
	 * Submit login action
	 * handle the user login request
	 * 
	 * @return "result", "error"
	 */
	public String login(){
		
		if(StringUtils.isEmpty(loginname)){
			//return ajax(Status.error, "用户名不能为空!");
			errorMessage = "用户名不能为空!";
			return ERROR;
		}
		if(StringUtils.isEmpty(password)){
			//return ajax(Status.error, "登录密码不能为空!");
			errorMessage = "登录密码不能为空!";
			return ERROR;
		}
		
		Member member = null;
		try{
			member = memberService.verifyMember(loginname, password);
			memberId = member.getId();
			username = member.getUsername();
			setSession(Member.MEMBER_ID_SESSION_NAME, memberId);
			return "success";
		} catch(PasswordMatchFailedException e){
			//return ajax(Status.error, "密码与用户名不匹配!");
			errorMessage = "密码与用户名不匹配!";
			return ERROR;
		} catch (MemberNotExistException e) {
			//return ajax(Status.error, "用户不存在，请先注册!");
			errorMessage = "用户名不存在，请先注册!";
			return ERROR;
		}
	}
	
	public String regist(){
		
		if(StringUtils.isEmpty(username)){
			return ajax(Status.error, "用户名不能为空!");
		}
		if(StringUtils.isEmpty(password)){
			return ajax(Status.error, "用户密码不能为空!");
		}
		if(StringUtils.isEmpty(email)){
			return ajax(Status.error, "用户邮箱不能为空!");
		}
		
		Member member = null;
		try{
			member = memberService.regist(username, email, password);
			memberId = member.getId();
		} catch(MemberAlreadyExistException e){
			return ajax(Status.error, "用户已经存在,不能重复注册!");
		} catch (InvalidParameterException e) {
			return ajax(Status.error, "用户注册参数不正确，请重新填写注册信息!");
		}
		return ajax(Status.success, memberId);
	}
	
}
